<?php declare(strict_types=1);


namespace App\Rpc\Traits\Service;

use App\Lib\Error\Response;
use App\Rpc\Model\Dao\UserBigDao;
use App\Rpc\Model\Dao\UserDao;
use Swoft\Bean\BeanFactory;
use Swoft\Db\DB;
use App\Rpc\Validator\UserBigValidator;
use Swoft\Validator\Exception\ValidatorException;

/**
 * 大货主相关方法
 * Trait UserBigTrait
 * @package App\Rpc\Traits\Service
 */
trait UserBigTrait
{
    /************************************************大货主部分**************************************************************/
    /**
     * 插入货主用户
     * @param array $data
     * @return array
     */
    public function userBigCreate(array $data): array
    {
        $diyValidator = [
            'UserBigValidator' => [ 'op' => 'add']
        ];
        try{
            $field = [
                'account' => '请输入货主端账号',
                'password' => '请输入货主端账号登录密码',
            ];

            //验证必须传输的参数
            $this->_requiredFields($field, $data);

            DB::beginTransaction();

            /** @var UserBigDao $UserDao */
            $UserDao = BeanFactory::getBean(UserBigDao::class);

            //添加item_id
            $data['item_id'] = create_item_id(uuid());
            //创建盐钥匙
            $data['salt'] = create_salt();
            //继续创建生成登录密码
            $data['password'] = create_password($data['password'], $data['salt']);
            //最后可以入库了,未冻结
            $data['frozen_state'] = $data['frozen_state']??0;

            //这里首先验证中间件
            validate($data, "UserBigValidator", [], $diyValidator);
            //@1入库
            $result = $UserDao->insert($data);

            if(!$result){
                throw new ValidatorException('创建货主端账号失败');
            }

            //处理关系对应
            $role_user_data = [
                'permission_value' => $data['permission_value']??'big',
                'user_id' => $result
            ];

            $this->_user_roles($role_user_data);

            //@4提交
            DB::commit();
            return Response::Info(['item_id' => $result]);
        }
        catch (\Exception $e){
            DB::rollBack();
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 用户更新
     * @param array $data
     * @return array
     */
    public function userBigUpdate(array $data): array
    {
        try{
            //添加item_id
            $id = $data['item_id']??null;
            unset($data['item_id']);

            /** @var UserBigDao $UserDao */
            $UserDao = BeanFactory::getBean(UserBigDao::class);

            if(isset($data['password'])){
                //处理下密码
                $userInfo = $UserDao->getByWhere(['item_id' => $id], ['salt']);
                $data['password'] = create_password($data['password'], $userInfo['salt']??null);
            }

            $result = $UserDao->update($id, $data);

            return Response::Info(['state' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 用户删除
     * @param array $data
     * @return array
     */
    public function userBigDelete(array $data): array
    {
        try{
            //添加item_id
            $id = $data['item_id']??null;
            unset($data['item_id']);

            /** @var UserDao $UserDao */
            $UserDao = BeanFactory::getBean(UserDao::class);
            $result = $UserDao->delete($id);

            return Response::Info(['state' => $result]);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 账号信息查询
     * @param array $data
     * @return array
     */
    public function userBigInfo(array $data): array
    {
        try{
            //添加item_id
            //根据item_id查询
            $id = $data['item_id']??null;
            unset($data['item_id']);
            $where['item_id'] = $id;
            /** @var UserDao $UserDao */
            $UserDao = BeanFactory::getBean(UserDao::class);
            $result = $UserDao->getByWhere($where);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 根据条件获取货主的人员
     * @param array $data
     * @return array
     */
    public function userBigList(array $data): array
    {
        try {
            //参数 where, per, p

            /** @var UserBigDao $dao */
            $dao = BeanFactory::getBean(UserBigDao::class);

            $p = $data['p']??1;
            $per = $data['per']??10;
            $where = $data['where']??[];

            $condition = [
                'where' => $where,
                'p' => $p,
                'per' => $per
            ];

            //默认过滤掉已经冻结的角色
            $result = $dao->getList($condition);
            return Response::Info($result);
        }
        catch (\Exception $e){
            return Response::FatalError($e->getCode(), $e->getMessage());
        }
    }
    /************************************************end大货主部分***********************************************************/
}
